<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:m="livcos.org/ns/map"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc c f m">

  <xsl:import href="../../web/style/doc/document.xsl" />
  <xsl:import href="../../web/style/base.xsl" />
  <xsl:import href="item.xsl" />
  <xsl:import href="../../web/style/web.xsl" />
  
  <xsl:output method="xml"/>
  
	<xsl:param name="ref" select="'../data/WebMap'"/>
	<xsl:param name="entity" select="'item_1'"/>
	<xsl:param name="map" select="$ref"/>
	<xsl:param name="item" select="$entity"/>
	
	<xsl:variable name="mapId" select="resolve-uri($map, $viewObjId)"/>
  <xsl:variable name="mapObj" select="document($map)/*"/>
  
  <xsl:variable name="MAX_MAIN_LABEL_LENGTH" select="40"/>
  <xsl:variable name="MAX_LABEL_LENGTH" select="30"/>
  <xsl:variable name="MAX_LINK_LABEL_LENGTH" select="40"/>
  
  <!-- resolve relative ref attributes -->
	<xsl:variable name="resolveObjUris" select="true()" />

  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="doc-available($map)">
        <xsl:variable name="itemEntity" select="$mapObj/m:body/*[@id = $item]"/>
        <xsl:variable name="data">
          <xsl:apply-templates select="$itemEntity" mode="prep">
            <xsl:with-param name="level" select="0"/>
          </xsl:apply-templates>
        </xsl:variable>
        <xsl:apply-templates select="$data/*" mode="run"/>
        <xsl:if test="not($data/*)">
          <xsl:call-template name="not_found">
            <xsl:with-param name="msg">Cannot find item '<xsl:value-of select="$item"/>' in map '<xsl:value-of
                select="$map"/>'!</xsl:with-param>
          </xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="not_found">
          <xsl:with-param name="msg">Cannot find map '<xsl:value-of
            select="$map"/>'!</xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="not_found">
    <xsl:param name="msg"/>
    <html>
      <head>
        <title>Not Found!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="{$toRootPath}/livcos.org/web/css/styles.css" rel="stylesheet" type="text/css"/>
      </head>
      <body>
      	<div>
      		<div style="width: 50%; margin: 100px 23% 0px 23%; padding: 2%; background-color: white;">
      			<p><xsl:value-of select="$msg"/></p>
      			<p style="text-align: right; margin-right: 20px;"><a href="javascript:history.back()">=> Back</a></p>
      		</div>
      	</div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="m:item" mode="run">
    <html id="{$viewObjId}">
      <head>
        <title><xsl:value-of select="m:label"/></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <xsl:call-template name="print_page_style"/>
        <xsl:call-template name="print_page_script"/>
      </head>
      <body>
        <div class="contentPanel">
          <table cellspacing="0" cellpadding="0">
            <tbody>
              <tr>
                <td class="left" rowspan="2">
                  <div class="rels">
                    <xsl:call-template name="apply_left"/>
                  </div>
                </td>
                <td class="top">
                  <div id="{@id}" class="mainItem">
                    <xsl:value-of select="substring(m:label, 0, $MAX_MAIN_LABEL_LENGTH)"/>
                    <xsl:if test="string-length(m:label) &gt;= $MAX_MAIN_LABEL_LENGTH">...</xsl:if>
                  </div>
                </td>
                <td class="right" rowspan="2">
                  <div class="rels">
                    <xsl:call-template name="apply_right"/>
                  </div>
                </td>
              </tr>
              <tr>
                <td class="center">
                  <div class="content">
                    <xsl:apply-templates select="doc:*"/>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div id="{$mapId}" class="map" onclick="nav('{$mapId}#item_1', null, event)">
          <xsl:value-of select="$mapObj/m:head/m:title"/>
        </div>
        <div id="_insert" style="display: none;">
          <form style="margin: 0px; padding: 0px;">
            <table class="ed_body">
              <tbody>
                <tr>
                  <td class="ed_label"><label for="label">Label:</label></td>
                  <td class="ed_entry">
                    <div style="position: relative">
                      <input name="label" type="text" autocomplete="off" value="-- label --"/>
                    </div>
                  </td>
                </tr>
                <tr>
                	<td class="ed_label"><label for="uri">URI:</label></td>
                  <td class="ed_entry">
                  	<div style="position: relative">
                      <input name="uri" type="text" autocomplete="off" value="{substring-before(@id, '#')}"/>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
						<div class="ed_footer">
							<input type="submit" value="Create" class="ed_action"/>
							<input type="button" onclick="this.form._panel.close();" value="Cancel" class="ed_action"/>
						</div>
          </form>
        </div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template name="print_page_style">
    <link href="css/nav_styles.css" rel="stylesheet" type="text/css" />
  </xsl:template>
  
  <xsl:template name="_print_page_style">
    <link href="{$toRootPath}/livcos.org/web/css/styles.css" rel="stylesheet" type="text/css"/>
    <link href="{$toRootPath}/livcos.org/web/css/status.css" rel="stylesheet" type="text/css"/>
    <link href="{$toRootPath}/livcos.org/web/css/edit.css" rel="stylesheet" type="text/css"/>
    <link href="css/nav.css" rel="stylesheet" type="text/css"/>
  </xsl:template>
  
  <xsl:template name="print_page_script">
    <script type="text/javascript" src="script/nav_scripts.js"/>
  </xsl:template>
  
  <xsl:template name="_print_page_script">
    <script type="text/javascript" src="../../web/script/util.js"/>
    <script type="text/javascript" src="../../web/script/E.js"/>
    <script type="text/javascript" src="../../web/script/init.js"/>
    <script type="text/javascript" src="../../web/script/gui.js"/>
    <script type="text/javascript" src="../../web/script/DragDrop.js"/>
    <script type="text/javascript" src="../../web/script/doc.js"/>
    <script type="text/javascript" src="../../web/script/Entity.js"/>
    <script type="text/javascript" src="../../web/script/EntityView.js"/>
    <script type="text/javascript" src="../../web/script/EditorPanel.js"/>
    <script type="text/javascript" src="../../web/script/Selection.js"/>
    <script type="text/javascript" src="../../web/script/editSetup.js"/>
  	<script type="text/javascript" src="../../web/script/EditJob.js"/>
  	<script type="text/javascript" src="../../web/script/edit.js"/>
  	<script type="text/javascript" src="../../web/script/editActions.js"/>
  	<script type="text/javascript" src="script/RelationDragDrop.js"/>
    <script type="text/javascript" src="script/nav_actions.js"/>
    <script type="text/javascript" src="script/nav.js"/>
  </xsl:template>
  
  <xsl:template name="apply_left">
    <xsl:apply-templates select="m:rels[not(@group) or (@group != 'main')]/*"/>
  </xsl:template>
  
  <xsl:template name="apply_right">
    <xsl:apply-templates select="m:rels[@group = 'main']/*"/>
  </xsl:template>
  
  <xsl:template match="m:rel">
    <div class="">
      <xsl:apply-templates select="m:item"/>
    </div>
  </xsl:template>
  
  <xsl:template match="/m:item/m:rels/m:rel">
    <table class="rel">
      <tbody>
        <tr>
          <xsl:choose>
            <xsl:when test="../@group = 'main'">
              <td class="right1">
                <xsl:apply-templates select="m:item"/>
              </td>
              <td class="right2">
                <xsl:apply-templates select="m:item/m:rels/m:rel"/>
              </td>
            </xsl:when>
            <xsl:otherwise>
              <td class="left2">
                <xsl:apply-templates select="m:item/m:rels/m:rel"/>
              </td>
              <td class="left1">
                <xsl:apply-templates select="m:item"/>
              </td>
            </xsl:otherwise>
          </xsl:choose>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  
  <xsl:template match="m:rel[m:item/@id = /m:item/@id]" priority="2">
  </xsl:template>
  
  <xsl:template match="m:item">
  	<xsl:variable name="level" select="count(ancestor::m:item)"/>
    <div id="{../@id}">
      <xsl:choose>
        <xsl:when test="@missing">
          <xsl:attribute name="class">item rel<xsl:value-of select="$level"/> missing</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">item rel<xsl:value-of select="$level"/></xsl:attribute>
          <xsl:attribute name="onclick">
            <xsl:text>nav('</xsl:text>
            <xsl:value-of select="@id"/>
            <xsl:text>', null, event);</xsl:text>
          </xsl:attribute>
          <xsl:if test="doc:link">
          	<xsl:variable name="uri">
          		<xsl:call-template name="link2Url">
          			<xsl:with-param name="link" select="doc:link[1]"/>
          		</xsl:call-template>
          	</xsl:variable>
            <xsl:attribute name="ondblclick">
              <xsl:text>nav('</xsl:text>
              <xsl:value-of select="@id"/>
              <xsl:text>','</xsl:text>
            	<xsl:choose>
            		<xsl:when test="starts-with($uri, $toRootPath)">
            			<xsl:value-of select="substring-after($uri, $toRootPath)"/>
            		</xsl:when>
            		<xsl:otherwise>
            			<xsl:value-of select="$uri"/>
            		</xsl:otherwise>
            	</xsl:choose>
            	<xsl:text>', event);</xsl:text>
            </xsl:attribute>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    	<xsl:if test="($level = 1) and ../@type">
    		<xsl:choose>
    			<xsl:when test="../../@group = 'main'">
    				<img src="img/{../@type}Right.png" style="margin: 1px -24px 0px 8px; float: left"/>
    			</xsl:when>
    			<xsl:otherwise>
    				<img src="img/{../@type}Left.png" style="margin: 1px 8px 0px -24px; float: right"/>
    			</xsl:otherwise>
    		</xsl:choose>
    	</xsl:if>
      <xsl:value-of select="substring(m:label, 0, $MAX_LABEL_LENGTH)"/>
      <xsl:if test="string-length(m:label) &gt;= $MAX_LABEL_LENGTH">...</xsl:if>
      <xsl:apply-templates select="../m:comment" mode="tooltip"/>
      <xsl:if test="not(../m:comment)">
        <xsl:apply-templates select="doc:paragraph[1]" mode="tooltip"/>
        <xsl:if test="not(doc:paragraph)"><xsl:apply-templates select="doc:*[1]" mode="tooltip"/></xsl:if>
      </xsl:if>
    </div>
  </xsl:template>
  
  <xsl:template match="m:comment | doc:*" mode="tooltip">
    <div class="tooltip"><xsl:value-of select="."/></div>
  </xsl:template>
  
  <xsl:template match="doc:link[. = '']" mode="tooltip">
    <div class="tooltip"><xsl:value-of select="@ref"/></div>
  </xsl:template>
  
  <xsl:template match="m:item/doc:link">
  	<xsl:variable name="ln">
  		<xsl:apply-imports/>
  	</xsl:variable>
    <div class="lnk" id="{@id}">
      <a>
      	<xsl:copy-of select="$ln/*/@*"/>
      	<xsl:value-of select="substring($ln/*, 0, $MAX_LINK_LABEL_LENGTH)"/>
      	<xsl:if test="string-length($ln/*) &gt;= $MAX_LINK_LABEL_LENGTH">...</xsl:if>
      </a>
    </div>
  </xsl:template>
  
</xsl:stylesheet>
